package com.blog.dazi.Service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class FriendService {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    public int makeFriend(String tel, String daziNum) {
        String sql = "insert into sys_friend (friend_tel, authen_dazi_number) values (?,?)";
        return jdbcTemplate.update(sql,tel,daziNum);
    }

    public int closeFriend(String tel, String daziNum) {
        String sql = "delete from sys_friend where friend_tel = ? and authen_dazi_number = ?";
        return jdbcTemplate.update(sql,tel,daziNum);
    }

    public List<Map<String, Object>> findByTelAndDazi(String tel, String daziNum) {
        String sql = "select * from sys_friend where friend_tel = ? and authen_dazi_number = ?";
        return jdbcTemplate.queryForList(sql,tel,daziNum);
    }

    public List<Map<String, Object>> list(String tel) {

        String sql = "select a.id userId,a.username,b.photo userPhoto from sys_user a, sys_user_photo b ," +
                "( select friend_tel,create_time from sys_friend where authen_dazi_number = (select dazi_number from sys_user " +
                " where phone_number  = ?) ) c where a.photo_id = b.id and a.phone_number = c.friend_tel order by  c.create_time desc";
        return jdbcTemplate.queryForList(sql,tel);
    }
}
